//
//  XYJGLViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/10.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJGLViewController.h"
#import "UIImageView+WebCache.h"
#import "XYJGLModel.h"
#import "XYJGLFrameModel.h"
#import "XYJGLTableViewCell.h"

@interface XYJGLViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *_yDataArr;
    NSMutableArray *_lDataArr;
    
    UITableView *_yTableView;
    UITableView *_lTableView;
    
    UIButton *yBtn;
    UIButton *lBtn;
    
}

@end

@implementation XYJGLViewController

-(instancetype)init{
    if (self = [super init]) {
        _yDataArr = [[NSMutableArray alloc] init];
        _lDataArr = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self navTitleView];
    [self initTable];
    [self netWorking];
    
}

-(void)navTitleView{
    UIView * titleView = [[UIView alloc]initWithFrame:RECT_MAKE((SCREEN_WIDTH - 160) / 2, 5, 160, 30)];
    titleView.backgroundColor = RGB(205, 219, 243);
    titleView.layer.masksToBounds = YES;
    titleView.layer.cornerRadius = 8;
    self.navigationItem.titleView = titleView;
    
    UIView *lineView = [[UIView alloc] initWithFrame:RECT_MAKE(80, 0, 1, 30)];
    lineView.backgroundColor = [UIColor grayColor];
    [titleView addSubview:lineView];
    
    yBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [yBtn setTitle:@"游玩概览" forState:UIControlStateNormal];
    [yBtn setTitleColor:RGB(27, 121, 255) forState:UIControlStateNormal];
    [yBtn setFrame:RECT_MAKE(0, 0, 80, 30)];
    [yBtn addTarget:self action:@selector(clickYbtn:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:yBtn];
    
    lBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [lBtn setTitle:@"最佳时间" forState:UIControlStateNormal];
    [lBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [lBtn setFrame:RECT_MAKE(80, 0, 80, 30)];
    [lBtn addTarget:self action:@selector(clickLbtn:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:lBtn];
    
}


-(void)initTable{
    _yTableView = [[UITableView alloc] initWithFrame:RECT_MAKE(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _yTableView.delegate = self;
    _yTableView.dataSource = self;
    _yTableView.showsVerticalScrollIndicator = YES;
    [self.view addSubview:_yTableView];
    _yTableView.hidden = NO;
    
    _lTableView = [[UITableView alloc] initWithFrame:RECT_MAKE(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _lTableView.delegate = self;
    _lTableView.dataSource = self;
    _lTableView.showsVerticalScrollIndicator = YES;
    [self.view addSubview:_lTableView];
    _lTableView.hidden = YES;
}


#pragma mark - 点击事件
-(void)clickYbtn:(id)sender{
    NSLog(@"点击游玩概览");
    [sender setTitleColor:RGB(27, 121, 255) forState:UIControlStateNormal];
    
    [lBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _lTableView.hidden = YES;
    _yTableView.hidden = NO;
}

-(void)clickLbtn:(id)sender{
    NSLog(@"点击最佳时间");
    [sender setTitleColor:RGB(27, 121, 255) forState:UIControlStateNormal];
    
    [yBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _yTableView.hidden = YES;
    _lTableView.hidden = NO;
}


#pragma mark - tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:_yTableView]) {
        return _yDataArr.count;
    }else return _lDataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XYJGLTableViewCell *cell = [XYJGLTableViewCell cellWithTableView:tableView];
    if ([tableView isEqual:_yTableView]) {
        cell.frameModel = _yDataArr[indexPath.row];
        
    }else cell.frameModel = _lDataArr[indexPath.row];
    return cell;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


#pragma mark - tableViewDelagate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:_yTableView]) {
        XYJGLFrameModel *frameModel = _yDataArr[indexPath.row];
        return frameModel.cellHeight;
    }else{
        XYJGLFrameModel *frameModel = _lDataArr[indexPath.row];
        return frameModel.cellHeight;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}



#pragma mark - 网络请求
-(void)netWorking{
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/wiki/destinations/%@.json",_selfID];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       //1.第一层遍历
        for (NSDictionary *dict in responseObject) {
            //2.第二层遍历
            for (NSDictionary *pages_dict in dict[@"pages"]) {
                //游玩概览
                if ([pages_dict[@"title"] isEqualToString:@"游玩指南"]) {
                    
                    //3.第三层遍历
                    for (NSDictionary *childDict in pages_dict[@"children"]) {
                        for (NSDictionary *sectionsDict in childDict[@"sections"]) {
                            XYJGLModel *model = [[XYJGLModel alloc] initWithDict:sectionsDict];
                            XYJGLFrameModel *frameModel = [[XYJGLFrameModel alloc] init];
                            frameModel.model = model;
                            [_yDataArr addObject:frameModel];
                        }
                    }
                }
                //最佳旅行时间
                if ([pages_dict[@"title"] isEqualToString:@"最佳旅行时间"]) {
                    //3.第三层遍历
                    for (NSDictionary *childDict in pages_dict[@"children"]) {
                        for (NSDictionary *sectionsDict in childDict[@"sections"]) {
                            XYJGLModel *model = [[XYJGLModel alloc] initWithDict:sectionsDict];
                            XYJGLFrameModel *frameModel = [[XYJGLFrameModel alloc] init];
                            frameModel.model = model;
                            [_lDataArr addObject:frameModel];
                        }
                    }
                }
            }
        }
        
        [_yTableView reloadData];
        [_lTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}

@end
